package com.example.finaltest.pwd;
import com.example.finaltest.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.finaltest.login.UserBean;
import com.example.finaltest.utils.BaseActivity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import static com.example.finaltest.utils.URLUtils.verifyAnswer_url;

public class PwdquestionActivity extends BaseActivity {
    TextView txt1;
    EditText et2;
    Button btn1;
    ImageView back;
    UserBean.DataBean userInfo;
    int id;
    String question, answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwdquestion);
        initView();
        setListener();
        setView();
    }


    private void setView() {

    }

    private void setListener() {
        btn1.setOnClickListener(listener);
        back.setOnClickListener(listener);
    }

    private void initView() {
        txt1 = findViewById(R.id.pwd_qs_tv2);
        et2 = findViewById(R.id.pwd_qs_et1);
        btn1 = findViewById(R.id.pwd_qs_btn);
        back = findViewById(R.id.pwd_qs_back);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int vid = v.getId();  // 获取到被点击的id
            switch (vid) {
                case R.id.pwd_qs_btn:
                    answer = et2.getText().toString();
                    Map<String, String> map = new HashMap<>();
                    map.put("id", id + "");
                    map.put("answer", answer);
                    postNetwork(verifyAnswer_url, map);

                case R.id.pwd_qs_back:
                    finish();

            }
        }
    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:   //获取成功
                Toast.makeText(PwdquestionActivity.this, "密保验证通过！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(PwdquestionActivity.this,PwdchangeActivity.class);
                startActivity(intent);

                break;
            default:   //其他，登录失败
                Toast.makeText(PwdquestionActivity.this, "密保验证失败！", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    protected void onResume() {
        id = getIntent().getIntExtra("id", 0);
        question = getIntent().getStringExtra("question");
        txt1.setText(question);
        super.onResume();
    }
}